function out = f_lda_training_testing_diff(ALL_vectors_training,All_testing)
Ngroup = length(All_testing);
%========
Xall = [];
for i=1:Ngroup
    for k=1:size(ALL_vectors_training{i},1)
        %         k
        Xall=[
            Xall
            ALL_vectors_training{i}(k,:) ];
    end
end
grand_avg = mean(Xall);
for i=1:Ngroup
    ALL_vectors_training{i}=ALL_vectors_training{i}-grand_avg;
    %     ALL_vectors_training{i}=zscore(ALL_vectors_training{i});
end
%==========
Xall = [];
for i=1:Ngroup
    for k=1:size(All_testing{i},1)
        Xall=[
            Xall
            All_testing{i}(k,:) ];
    end
end
grand_avg = mean(Xall);
for i=1:Ngroup
    All_testing{i}=All_testing{i}-grand_avg;
end
%==============

% training:
dclass = [];
Xt = [];
for Gy = 1:Ngroup
    trainsB = ALL_vectors_training{Gy};
    Xt = [
        Xt
        trainsB];
    dclass =[dclass Gy.*ones(1,size(trainsB,1))];
end % end Gy
no_dims = Ngroup-1;
[mappedX, mapping] = lda(Xt,dclass, no_dims);
%testing

CM = zeros(Ngroup,Ngroup);
for true_class = 1:Ngroup
    pause(0.01)
    trainsA = All_testing{true_class};
    [x9 y9]=size(trainsA);
    for itrial = 1:x9% go through all vectors in A.
        train1 =trainsA(itrial,:); % train 1 is to be classified; is an average of the previous 3 trains
        reduced_X = train1*mapping.M;
        for ic =  1:Ngroup
            Ind0 = find(dclass==ic);
            this_class = mappedX(Ind0,:);
            dist(ic) = 0;
            for jj = 1:size(this_class,1)
                dist(ic) =  dist(ic) + ( sqrt(sum((reduced_X-this_class(jj,:)).^2)));
            end
            dist(ic)= dist(ic)./size(this_class,1);
        end
        [Y Indmin ]= min(dist);
        CM(true_class,Indmin) = CM(true_class,Indmin) + 1;
        if true_class==Indmin
            out.rec_correct(true_class,itrial) = 1;
        else
            out.rec_correct(true_class,itrial) = 0;
        end
        clear MeanC dist
    end % itrial
    %     legend('-5','0','5')
    %     set(gca,'fontsize',9,'xtick',-20:2:20)
end % true_class
out.CM = CM;
out.PC = sum(diag(CM))./sum(sum(CM));






